package cn.e4j.modular.crm.controller.base;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.LangUtils;
import cn.e4j.model.exception.ServiceException;
import cn.e4j.modular.crm.entity.GwekT;
import cn.e4j.modular.crm.service.impl.Gwei014ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 月汇率控制类
 *
 * @author yuan_rz
 * @Date 2019/01/10 16:22
 */
@Controller
@RequestMapping("/base/gwei014")
public class Gwei014Controller extends BaseController {

    @Autowired
    private Gwei014ServiceImpl gwei014Service;

    /**
     * 跳转页面
     */
    @RequiresPermissions("base:gwei014:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/base/gwei014.html";
    }

    /**
     * 查询列表数据
     */
    @RequestMapping("/getListData")
    @RequiresPermissions("base:gwei014:init")
    @ResponseBody
    public ResponseData getListData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwei014Service.getListData(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 导出Excel
     */
    @RequestMapping(value="/exportListData",method=RequestMethod.GET)
    @RequiresPermissions("base:gwei014:init")
    public void exportListData(ModelMap map, HttpServletRequest request, HttpServletResponse response){

    }

    /**
     * 添加数据
     */
    @RequestMapping("/addData")
    @RequiresPermissions("base:gwei014:init")
    @ResponseBody
    public ResponseData addData(GwekT gwekT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwekT.setGWEKENT(shiroUser.getDBC());
        if(gwei014Service.existedTheParams(gwekT)){
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode() ,
                    BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        }
        gwei014Service.addData(gwekT);
        return SUCCESS_TIP;
    }

    /**
     * 修改数据
     */
    @RequestMapping("/editData")
    @RequiresPermissions("base:gwei014:init")
    @ResponseBody
    public ResponseData editData(GwekT gwekT,@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        GwekT oldGwekT = new GwekT();
        oldGwekT.setGWEKENT(gwekT.getGWEKENT());
        oldGwekT.setGWEK001(inDto.getString("oldGWEK001"));
        oldGwekT.setGWEK002(inDto.getString("oldGWEK002"));
        if(!(gwekT.getGWEK001().equals(oldGwekT.getGWEK001())&&
                gwekT.getGWEK002().equals(oldGwekT.getGWEK002()))){
            if(gwei014Service.existedTheParams(gwekT)){
                return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode() ,
                        BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
            }
        }
        gwei014Service.editData(gwekT,oldGwekT);
        return SUCCESS_TIP;
    }

    /**
     * 删除数据
     */
    @RequestMapping("/deleteData")
    @RequiresPermissions("base:gwei014:init")
    @ResponseBody
    public ResponseData deleteData(GwekT gwekT) {
        gwei014Service.deleteData(gwekT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("base:gwei014:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwekT> gwekTList = inDto.getReqList(GwekT.class);
        gwei014Service.deleteDataBatch(gwekTList);
        return SUCCESS_TIP;
    }

    /**
     * 查询列表数据
     */
    @RequestMapping("/getGwacList")
    @ResponseBody
    public ResponseData getGwacList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwei014Service.getGwacList(page, inDto);
        return new PageReData(page, result);
    }
}
